import numpy as np
from scipy.optimize import leastsq
import matplotlib.pyplot as plt

# 定义一系列坐标点
points = np.array([(114.290774,30.582743),
(114.264954,30.603727),
(114.291167,30.583053),
(114.289372,30.581370),
(114.294876,30.594710),
(114.279152,30.577272),
(114.279101,30.577162),
(114.295168,30.593389),
(114.276543,30.576963),
(114.282424,30.577225),
(114.284701,30.607250),
(114.285278,30.607086),
(114.282425,30.577107),
(114.267956,30.578334),
(114.266550,30.606136),
(114.295692,30.588768),
(114.295736,30.588725),
(114.276642,30.609140),
(114.295222,30.598314),
(114.257804,30.591326),
(114.280588,30.576043),
(114.292942,30.582417),
(114.277866,30.609556),
(114.277048,30.575558),
(114.295062,30.600001),
(114.293222,30.591355),
(114.291278,30.585793),
(114.281970,30.605884),
(114.282162,30.605904),
(114.260861,30.591677),
(114.292481,30.597467),
(114.262369,30.586348),
(114.272755,30.606313),
(114.290739,30.584074),
(114.281810,30.578650),
(114.275858,30.607172),
(114.262595,30.600168),
(114.292664,30.598663),
(114.280300,30.578033),
(114.275320,30.607304),
(114.294374,30.592547),
(114.275280,30.607335),
(114.262943,30.584171),
(114.270099,30.606141),
(114.259837,30.591596),
(114.259999,30.590279),
(114.275848,30.607586),
(114.259722,30.591372),
(114.262158,30.584735),
(114.292059,30.600754),
(114.287957,30.600103),
(114.281296,30.580976),
(114.269028,30.602774),
(114.283250,30.581326),
(114.285029,30.581981),
(114.289520,30.585602),
(114.262883,30.589195),
(114.288212,30.601105),
(114.290080,30.586090),
(114.292385,30.591015),
(114.283448,30.604689),
(114.264363,30.585099),
(114.263114,30.586520),
(114.290840,30.585927),
(114.291220,30.586504),
(114.290873,30.585918),
(114.288300,30.582970),
(114.292888,30.593743),
(114.292216,30.596750),
(114.291672,30.598159),
(114.290379,30.600190),
(114.283999,30.580196),
(114.292423,30.596650),
(114.291073,30.585646),
(114.292713,30.588973),
(114.267651,30.592817),
(114.273015,30.600042),
(114.287163,30.593047),
(114.287521,30.591133),
(114.277461,30.601832),
(114.286364,30.597694),
(114.286836,30.597207),
(114.266147,30.589399),
(114.266061,30.589276),
(114.267638,30.585831),
(114.281782,30.582693),
(114.289733,30.594159),
(114.273800,30.603134),
(114.289553,30.595947),
(114.282058,30.582049),
(114.282807,30.582267),
(114.274715,30.581319),
(114.273814,30.603745),
(114.271004,30.603001),
(114.290174,30.589020),
(114.288007,30.599844),
(114.284050,30.582313),
(114.290925,30.591648),
(114.264630,30.597587),
(114.283002,30.581747),
(114.276286,30.591012),
(114.274853,30.591239),
(114.277998,30.590115),
(114.276726,30.595661),
(114.277879,30.589474),
(114.273483,30.593228),
(114.276756,30.588540),
(114.280217,30.595978),
(114.277291,30.588265),
(114.272032,30.593657),
(114.277200,30.597572),
(114.271574,30.593994),
(114.276551,30.597574),
(114.271491,30.594132),
(114.279980,30.587836),
(114.282372,30.588815),
(114.283275,30.595674),
(114.270578,30.595157),
(114.269966,30.591105),
(114.283449,30.589183),
(114.269700,30.591300),
(114.270015,30.594929),
(114.268576,30.592237),
(114.285531,30.594489),
(114.281594,30.585954),
(114.258361,30.586910),
(114.257145,30.590174),
(114.257082,30.590233),
(114.262673,30.604595),
(114.267875,30.578485),
(114.258727,30.584514),
(114.264061,30.605810),
(114.256289,30.587866),
(114.256111,30.587592),
(114.262822,30.606014),
(114.267316,30.577228),
(114.257442,30.583271),
(114.258530,30.581591),
(114.274835,30.610550),
(114.270327,30.610883),
(114.268950,30.611457),
(114.278209,30.611471),
(114.280864,30.610731),
(114.263570,30.609800),
(114.268137,30.581178),
(114.285801,30.609183),
(114.276950,30.612803),
(114.260368,30.608623),
(114.273358,30.613412),
(114.284327,30.610954),
(114.288338,30.585220),
(114.255131,30.603690),
(114.263480,30.611760),
(114.274864,30.614094),
(114.285859,30.610674),
(114.289809,30.607732),
(114.289955,30.607635),

])

# 提取x和y坐标
x_coords, y_coords = points[:, 0], points[:, 1]

# 定义拟合圆的函数
def calc_R(xc, yc):
    """计算所有点到圆心 (xc, yc) 的距离"""
    return np.sqrt((x_coords - xc)**2 + (y_coords - yc)**2)

def f_2(c):
    """计算所有点到圆心的距离与平均距离的差"""
    Ri = calc_R(*c)
    return Ri - Ri.mean()

# 初始猜测圆心 (x, y) 和半径
x_m, y_m = x_coords.mean(), y_coords.mean()
initial_guess = [x_m, y_m]

# 使用最小二乘法拟合圆
center, ier = leastsq(f_2, initial_guess)

# 拟合结果
xc, yc = center
Ri = calc_R(xc, yc)
R = Ri.mean()

print(f"拟合圆的圆心: ({xc}, {yc})")
print(f"拟合圆的半径: {R}")

# 绘制结果
fig, ax = plt.subplots()
ax.plot(x_coords, y_coords, 'o')
circle = plt.Circle((xc, yc), R, color='r', fill=False)
ax.add_patch(circle)
plt.xlabel('X')
plt.ylabel('Y')
plt.legend()
plt.axis('equal')
plt.grid(True)
plt.show()